package cosw.com.shanghaica;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.cosw.sdkShanghaiCA.EnumError;
import com.cosw.sdkShanghaiCA.SHCAProvider;

public class InitDeviceActivity extends ActionBarActivity {

  @BindView(R.id.tv_super_pin)
  TextView tvSuperPin;
  @BindView(R.id.tv_admin_pin)
  TextView tvAdminPin;
  @BindView(R.id.et_super_pin)
  EditText etSuperPin;
  @BindView(R.id.et_admin_pin)
  EditText etAdminPin;
  @BindView(R.id.btn_go)
  Button btnGo;

  private SHCAProvider provider;

  private Context mContext;

  private SharedPreferences prefs;


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_init_device);
    ButterKnife.bind(this);

    prefs = PreferenceManager
        .getDefaultSharedPreferences(this.getApplication());

    mContext = this;

    provider = SHCAProvider.getInstance(this.getApplication());
    provider.setEnv(mContext);
  }

  @OnClick(R.id.btn_go)
  public void onViewClicked() {
    String superPin = etSuperPin.getText().toString();
    String adminPin = etAdminPin.getText().toString();

    int result = provider.initDevice(superPin, adminPin, 0);
    if (EnumError.SUCCESS.getResCode() == result) {
      Editor editor = prefs.edit();
      editor.putString("superPin", superPin);
      editor.putString("adminPin", adminPin);
      editor.apply();
      Intent intent = new Intent(mContext, DeviceManageActivity.class);
      startActivity(intent);
      finish();
    }
  }
}
